package cn.sharp.bbs.controller.security;

import cn.sharp.bbs.common.Result;
import cn.sharp.bbs.common.TokenUtils;
import cn.sharp.bbs.entity.BO.LoginEntity;
import cn.sharp.bbs.entity.BO.RegisterEntity;
import cn.sharp.bbs.service.LoginService;
import cn.sharp.bbs.service.RegisterService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/register")
public class RegisterController {
    @Resource
    RegisterService registerService;
    @Resource
    LoginService loginService;

    /**
     * 用户注册
     * @param entity 注册实体
     * @return 注册结果
     */
    @PostMapping("")
    public Result register(@RequestBody RegisterEntity entity){
        String error_msg = registerService.checkRegistry(entity);
        if(error_msg!=null)
            return Result.fail(Result.ERR_CODE_BUSINESS, error_msg);
        Long uid = registerService.doRegister(entity);
        String token = loginService.doLogin(new LoginEntity(uid, entity.getAccount(), entity.getPasswd()));
        return Result.success((Object) token);
    }
}
